import { defineStore } from 'pinia'
import type { AppRouteRecordRaw, Menu } from '@/router/types'
import { asyncRoutes } from '@/router/routes'
import {filter} from "@/utils/helper/treeHelper.ts";


interface PermissionState {
  // 后台菜单列表
  backMenuList: Menu[]
}

export const usePermissionStore = defineStore('app-permission', {
  state: (): PermissionState => ({
    backMenuList: []
  }),
  getters: {
    getBackMenuList(state): Menu[] {
      return state.backMenuList
    },
  },
  actions: {
    setBackMenuList(list: Menu[]) {
      this.backMenuList = list
    },
    // 构建路由
    buildRoutesAction(): Promise<AppRouteRecordRaw[]> {
      // 路由过滤器 在 函数filter 作为回调传入遍历使用
      const routeFilter = () => true
      let routes: AppRouteRecordRaw[] = []
      routes = filter(asyncRoutes, routeFilter)

      // this.setBackMenuList(routes)
      return Promise.resolve(routes)
    }
  }
})
